package com.hktf.uilib.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hktf.uilib.R;

/**
 * 单例土司工具类
 * @author zhouyou
 */
public class ToastUtils {
	
    public static Toast mToast;

    /**
     * 显示吐司
     * @param context
     * @param message
     */
    public static void showToast(final Context context, final String message){
        if (mToast == null){
            mToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        }else{
            mToast.setText(message);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }
    /**
     * 显示吐司
     * @param context
     * @param message
     */
    public static void showToast(final Context context, final String message,int length){
        if (mToast == null){
            mToast = Toast.makeText(context, message, length);
        }else{
            mToast.setText(message);
            mToast.setDuration(length);
        }
        mToast.show();
    }
    /**
     * 显示吐司
     * @param context
     * @param messageResId
     */
    public static void showToast(final Context context, final int messageResId){
        if (mToast == null){
            mToast = Toast.makeText(context, messageResId, Toast.LENGTH_SHORT);
        }else{
            mToast.setText(messageResId);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    public static Toast makeImgAndTextToast(Context context,
                                              Drawable drawable, CharSequence text, int duration) {
        Toast result = new Toast(context);

        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(R.layout.my_toast, null);

        ImageView img = (ImageView) v.findViewById(R.id.mytoast_img);
        img.setBackgroundDrawable(drawable);
        TextView tv = (TextView) v.findViewById(R.id.mytoast_text);
        tv.setText(text);

        result.setView(v);
        // setGravity方法用于设置位置，此处为屏幕底部
        result.setGravity(Gravity.BOTTOM, 0, 120);
        result.setDuration(duration);

        return result;
    }
}
